import 'dart:io';

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/form/text_area_input_form.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:flutter/material.dart';

import 'default_bottom_sheet.dart';

///调整单位
showAddUOMInputBottomSheet(BuildContext context,
        {String name, ValueChanged<String> onDone}) =>
    showDefaultBottomSheet(context,
        child: _OneInputWidget(
          onClick: onDone,
          title: S.current.name,
          btnText: S.current.add,
          initText: name,
        ),
        leading: name == null
            ? null
            : DefaultBottomSheetLeadingButton(
                title: S.current.delete,
                onTap: name == null
                    ? null
                    : () {
                        Navigator.of(context).pop(true);
                      },
              ),
        isScrollControlled: true,
        title: S.current.add_uom);

class _OneInputWidget extends StatefulWidget {
  final ValueChanged<String> onClick;
  final String title;
  final String btnText;
  final String initText;

  const _OneInputWidget(
      {Key key, this.onClick, this.title, this.btnText, this.initText})
      : super(key: key);

  @override
  _OneInputWidgetState createState() => _OneInputWidgetState();
}

class _OneInputWidgetState extends State<_OneInputWidget> {
  TextEditingController reasonController = TextEditingController();
  FocusNode reasonFocusNode = FocusNode();

  bool get isEnable => reasonController.text.length > 0;

  @override
  void initState() {
    super.initState();
    if (widget.initText != null) {
      reasonController.text = widget.initText;
    }
    reasonController.addListener(() {
      setState(() {});
    });
  }

  @override
  void dispose() {
    super.dispose();
    reasonController.dispose();
    reasonFocusNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppSpaces.h20,
        InputField(
          tip: widget.title,
          controller: reasonController,
          focusNode: reasonFocusNode,
        ),
        SizedBox(
          height: getDp(30),
        ),
        NormalButton(
          onTap: isEnable
              ? (() {
                  if (widget.onClick != null) {
                    widget.onClick?.call(reasonController.text);
                  } else {
                    Navigator.of(context).pop(reasonController.text);
                  }
                })
              : null,
          title: widget.btnText,
        ),
      ],
    );
  }
}
